/* 
 * Coded by David Bruno Cortarello <Nomius>. Redistribute under the terms of
 * the BSD-lite license. Bugs, suggests, nor projects: dcortarello@gmail.com.
 *
 * Program: Limshsql
 * Version: 2.0
 *
 *
 * Copyright (c) 2011, David B. Cortarello
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice
 *     and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright notice
 *     and the following disclaimer in the documentation and/or other materials
 *     provided with the distribution.
 *   * Neither the name of Kwort nor the names of its contributors may be used
 *     to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <dlfcn.h>
#include <time.h>
#include <limits.h>
#include "symloader.h"

static void *backend_handler;

/* Superduper macro! :-P */
#define dynamic_symbol_resolution(fn, sfn) { \
	dlerror(); \
	fn = dlsym(backend_handler, sfn); \
	if ((error = dlerror())) { \
		fprintf(stderr, "Couldn't find symbol %s: [%s]\n", sfn, error); \
		dlclose(backend_handler); \
		return 1; \
	} \
}

int symloader(char *backend)
{
	char ObjectPath[PATH_MAX];
	char *error = NULL;

	/* Load the desired backend */
	sprintf(ObjectPath, "%s/lib%s.so", PLUGINS_LOCATION, backend);
	if ((backend_handler = dlopen(ObjectPath, RTLD_NOW)) == NULL) {
		perror("Can't load backend");
		return 1;
	}

	/* Ok, let's make this clear...
	 * This is a fancy way to do this fast, but the fact that this is not C89 
	 * compliant drove me crazy for like a whole night...
	 * But I'm in a part of my life that I don't give a damn about doing it 
	 * 100% right, so screw it and let's do it fast and dirty */
	dynamic_symbol_resolution(ReadDBConfig, "ReadDBConfig");
	dynamic_symbol_resolution(ConnectDB, "ConnectDB");
	dynamic_symbol_resolution(CloseDB, "CloseDB");
	dynamic_symbol_resolution(InitializeTable, "InitializeTable");
	dynamic_symbol_resolution(RegisterAction, "RegisterAction");

	return 0;
}


void symunloader(void) {
	dlclose(backend_handler);
}

